//
//  UIColor+Extension.m
//  MiaMind
//
//  Created by Vill on 2023/11/23.
//

#import "UIColor+Extension.h"

@implementation UIColor (Extension)

+ (instancetype)colorHex:(NSString *)hex{
    return [UIColor colorHex:hex alpha:1.0];
}

+ (instancetype)colorHex:(NSString *)hex alpha:(CGFloat)alpha{
    //去掉前后空格换行符
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    if ([cString length] < 6) return [UIColor whiteColor];

    if ([cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    } else if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }

    if ([cString length] != 6) return [UIColor whiteColor];

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:alpha];
}

/**
 *  @brief  渐变颜色
 *
 *  @param from           开始颜色
 *  @param to           结束颜色
 *  @param direction    渐变方向
 *  @param range        渐变距离（竖向为高度，横向为宽度）
 *
 *  @return 渐变颜色
 */
+ (UIColor*)gradient:(UIColor*)from to:(UIColor*)to dir:(Direction)direction range:(int)range{
    if (direction == Horizontal) {
        CGSize size = CGSizeMake(range, 1);
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
        
        NSArray* colors = [NSArray arrayWithObjects:(id)from.CGColor, (id)to.CGColor, nil];
        CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
        CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(size.width, 0), 0);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorspace);
        UIGraphicsEndImageContext();
        return [UIColor colorWithPatternImage:image];
    } else if (direction == Vertical) {
        CGSize size = CGSizeMake(1, range);
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
        
        NSArray* colors = [NSArray arrayWithObjects:(id)from.CGColor, (id)to.CGColor, nil];
        CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
        CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorspace);
        UIGraphicsEndImageContext();
        return [UIColor colorWithPatternImage:image];
    } else {
        return [UIColor blackColor];
    }
}

/**
 用UIColor颜色生成一张纯色图片
 
 @param color UIColor颜色对象
 @return UIImage对象
 */
+ (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

/**
 从一张单色滤镜图片上面获取颜色
 
 @param image UIImage对象
 @return UIColor对象
 */
+ (UIColor *)colorWithUIImage:(UIImage *)image{
    return [self colorWithPatternImage:image];
}



@end
